
/*
 * WebPageRegistrationServlet.java
 *
 * Created on 29 December, 2007, 6:21 PM
 */

import database.RecordSearch;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import guiregistration.*;
import guireportgenerator.RequestHandler;

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Hashtable;

import javax.servlet.*;
import javax.servlet.http.*;
import utility.MasterPLOperations;
import utility.Input;

/**
 * @author Hemant
 * @version
 * This servlet is dedicated for handling request for web page registration i.e. for registering webreport
 * in POOL.
 * It passes the HTML page to HTMLParseMain class which then proceeds registration.  
 */
public class WebPageRegistrationServlet extends HttpServlet
{
    String username,password;
    GetIdName get =  null;
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
        String HTMLText = request.getParameter("myTextarea0").replace("\"", "\'");
        
        HttpSession mysession = request.getSession();
        String objectname = (String)mysession.getAttribute("objectname");
        
        PrintWriter out = null;
        
        try
        {
        	username = (String)mysession.getAttribute("username");
        	password = (String)mysession.getAttribute("password");
            
        	out = response.getWriter();
        	
	        get = new GetIdName();
	        HtmlParseMain htmlparser = null;
	        String obj = null;
	        RequestHandler req = new RequestHandler();
	        
	        /*try
	        {
	        	obj = get.getItem(objectname);
	        
		        req.setUsername(username);
		        req.setRequestPage(obj);
		        req.setMaster("");
		        
				if(req.isUrl()) 
				{
					String page = req.getFileName();
					htmlparser = new HtmlParseMain(page, HTMLText, db);				
				}
				else
				{
					htmlparser = new HtmlParseMain(HTMLText, db);
				}
	        }
	        catch (Exception e) { 	
	        	htmlparser = new HtmlParseMain(HTMLText, db);	        	
			}
	        
	        String webpage = this.getwebpageObject();*/
			
	        htmlparser = new HtmlParseMain(HTMLText);
	        htmlparser.setUsername(username);
	        htmlparser.setPassword(password);
	        htmlparser.parse();
	        String registered_object = HTMLParseLister.start_tag;
	        
	        /*if(obj != null)
	        {
	        	MasterPLOperations mp = new MasterPLOperations(username,password, db);
	        	mp.setMaster(webpage);
	        	mp.setremoveProperty(webpage, obj);
	        	mp.removeProperty();
	        	mp.setNewProperty(webpage, obj, registered_object, "1");
	        	mp.insertProperty();
	        	req.setMaster(obj);
	        }*/
	        
        	String url = null;
        	req.setMaster("");
        	req.setRequestPage(registered_object);
        	
        	boolean isURL = req.isUrl();
        	
        	if(isURL)
            {
            	url = "http://"+Input.URL+req.getFile();
            	System.out.println("The request url is  "+url);
            	out.write("<html><head>");
            	out.write("<META HTTP-EQUIV='Refresh' CONTENT='0;URL="+url+">");
            	out.write("</head><body></body></html>");
            	out.flush();
        	}
            else
            {
            	url = req.getHtml();
            	out.write(url);
            	out.flush();
            }
        	
        	/*url = req.getFileContent(req.getFileName());
        	out.write(url);
        	out.flush();
        	*/
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        finally
        {
        	get = null;            
        }
    }
    
    public String getwebpageObject()
    {
    	String webpage = get.getId("webpage");
		String userId = get.getId(username);
		
		Hashtable<Object, Object> condition = new Hashtable<Object, Object>();
		condition.put("mid", userId);
		condition.put("pid", webpage);
		condition.put("td", "null");
		
		RecordSearch rs = new RecordSearch();
		rs.setConditionMap(condition);
		ResultSet rs1 = rs.getResultSet();
		String pv = null;
		
		try
    	{
			while(rs1.next())
				pv = get.getItem(rs1.getString("PV"));
			
			rs1.close();
		}
    	catch (Exception e)
		{
			e.printStackTrace();
		}
    	
    	return pv;
    }
}
